package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.pojo.Orders;
import com.itheima.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        return  ordersService.submit(orders);
    }

    //  查询用户的最新订单
    @GetMapping("/userPage")
    public R userPage(Integer page, Integer pageSize) {
        R r = ordersService.userPage(page, pageSize);
        return r;
    }

    //查询订单的状态信息
    @GetMapping("/findStatusById")
    public R findStatusById(String orderId){

        //1.根据Id查询订单状态
        Orders order = ordersService.getById(orderId);

        //2.判断状态是否为2
        if(order.getStatus() == 2){

            return R.success("支付成功");//code = 1
        }
        //3.如果状态为已支付。直接跳转 返回code 1
        return R.error("等待支付"); //code = 0
    }
}
